<template>
    <div id="carType">
        <el-row style="height: 100%;border: 1px solid #DCDFE6;margin-top: 10px">
            <el-col :span="6" style="border-right: 1px solid #DCDFE6; min-height:500px;">
                <div class="grid-content bg-purple">
                    <el-tree :data="carTypes" :props="defaultProps"  @node-click="handleNodeClick">
                    </el-tree>
                </div>
            </el-col>
            <el-col :span="17" style="margin-left: 10px;padding-top: 10px">
                <!--工具条-->
                <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
                    <el-form :inline="true" :model="filters">
                        <el-form-item>
                            <el-input v-model="filters.keyword" placeholder="关键字"></el-input>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" v-on:click="getList">查询</el-button>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" @click="handleAdd">新增</el-button>
                        </el-form-item>
                    </el-form>
                </el-col>

                <!--列表-->
                <el-table :data="datas" highlight-current-row  @selection-change="selsChange" style="width: 100%;">
                    <el-table-column type="selection" >
                    </el-table-column>
                    <el-table-column prop="name" label="标题" width="120" sortable>
                    </el-table-column>
                    <el-table-column prop="icon" label="icon" width="120" sortable>
                    </el-table-column>
                    <el-table-column prop="description" label="描述" width="120" sortable>
                    </el-table-column>
                    <el-table-column label="操作" width="150">
                        <template slot-scope="scope">
                            <el-button size="small" @click="handleEdit(scope.row)">编辑</el-button>
                            <el-button type="danger" size="small" @click="handleDel(scope.row.id)">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>

                <!--工具条-->
                <el-col :span="24" class="toolbar">
                    <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
                </el-col>

            </el-col>
        </el-row>

        <!--新增界面-->
        <el-dialog title="新增" :visible.sync="addFormVisible"  :close-on-click-modal="false">
            <el-form :model="addForm" label-width="80px"  ref="addForm">
                <el-form-item label="分类标题" prop="name">
                    <el-input v-model="addForm.name" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="icon" prop="icon">
                    <el-input v-model="addForm.icon" auto-complete="off"></el-input>
                </el-form-item>
                <!-- <el-form-item label="父类型" prop="pid">
                    <el-cascader v-model="addForm.pid" placeholder="请选择"
							:options="carTypes"
							:props="{
                              children:'children',
							  checkStrictly: true,
							  label:'name',
							  value:'id'
							}" clearable>
                    </el-cascader>
                </el-form-item> -->
                <el-form-item label="排序" prop="index">
                    <el-input v-model="addForm.index" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="描述" prop="description">
                    <el-input v-model="addForm.description" auto-complete="off"></el-input>
                </el-form-item>

            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="addFormVisible = false">取消</el-button>
                <el-button type="primary" @click.native="addSubmit" :loading="saveloading" >提交</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    import carTypeApi from '@/api/car/carType'
    export default {
        data() {
            return {
                addForm:{
                    name:"",
                    icon:"",
                    index:"",
                    description:"",
                    pid:""
                },
                addFormVisible:false,
                sels:[],
                filters:{
                    keyword:""
                },
                datas:[],
                carTypes:[],
                pid:0,
                ids:[],
                defaultProps: {
                    children: 'children',
                    label: 'name'
                },
                saveloading:false
            }
        },
        methods:{
            handleAdd(){
                this.addForm = {
                    name:"",
                    icon:"",
                    index:"",
                    description:"",
                    pid:""
                }
                this.addFormVisible = true;
            },
            addSubmit(){
                this.addForm.pid = this.pid
                this.saveloading = true
                // 提交
                carTypeApi.addOrUpdateCarType(this.addForm)
                    .then(response => {
                        this.addFormVisible = false;
                        this.saveloading = false
                        this.$message({
                            message: '保存成功',
                            type: 'success'
                        });
                        this.getTreeData();
                    }).catch(response => {
                        // this.addFormVisible = false;
                        this.saveloading = false
                        this.$message({
                            message: response.data.message,
                            type: 'error'
                        });
                    })
            },
            getChildrenByPid(pid){
                carTypeApi.getChildren(pid)
                    .then(response => {
                        this.datas = response.data.resultObj
                    })
            },
            batchRemove(){
                this.$confirm('此操作将永久删除这些车辆类型记录, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                    }).then(() => {
                        carTypeApi.batchDelete(this.ids)
                            .then(response => {
                                this.$message({
                                    message: '删除成功',
                                    type: 'success'
                                });
                                this.getTreeData();
                                this.getChildrenByPid(this.pid)
                            }).catch(response => {
                                this.$message({
                                    message: '删除失败',
                                    type: 'error'
                                });
                            })
                    }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });          
                });
            },
            handleEdit(row){
                this.addForm = row
                this.addFormVisible = true
            },
            handleDel(id){
                this.$confirm('此操作将永久删除该车辆类型记录, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                    }).then(() => {
                        carTypeApi.delete(id)
                            .then(response => {
                                this.$message({
                                    message: '删除成功',
                                    type: 'success'
                                });
                                this.getTreeData();
                                this.getChildrenByPid(this.pid)
                            }).catch(response => {
                                this.$message({
                                    message: '删除失败',
                                    type: 'error'
                                });
                            })
                    }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });          
                });
            },
            selsChange(value){
                this.sels = value
                this.sels.forEach(c => {
                    this.ids.push(c.id)
                });
            },
            getList(){

            },
            handleNodeClick(row){
                this.datas = row.children;
                this.pid = row.id
            },
            getTreeData(){
                carTypeApi.getTree(0)
                    .then(response => {
                        this.carTypes = response.data.resultObj;
                        this.getChildrenByPid(this.pid)
                    })
            }
        },
        mounted(){
            //对carTypes数据赋值
           this.getTreeData();
        }
    };
</script>
<style scoped>
    .el-row {
        margin-bottom: 20px;
        height: 100%;
    }
    :last-child {
        margin-bottom: 0;
    }
    #carType{
        margin: 20px 16px;
    }
    #carType el-col {
        border: 1px solid red;
        border-radius: 4px;
    }
    .grid-content {
        border-radius: 4px;
        min-height: 36px;
    }
    .toolbar{
        background-color: #F4F3F4;
        height: 60px;
        padding-top: 10px;
        padding-left: 10px;
    }
</style>